import csv
from matplotlib import pyplot as plt
from datetime import datetime
import matplotlib.dates as mdate
import pandas as pd
# 此程序能完成每时每刻对应的PM2.5 的高度和变化，但是数据太过浓密，导致可视化后并不好看
filename = 'D:\\Users\\kai\\PycharmProjects\\python_mat\\Fiberhome_DataAnalysis\\co2.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    dates,highs,winds = [],[],[]
    for row in reader:
        try:
            current_date = datetime.strptime(row[1]+'-'+row[2]+'-'+row[3]+'-'+row[4],"%Y-%m-%d-%H")
            high = (row[5])
            wind = row[10]
        except ValueError:
            print(current_date,'missing data')
        else:
            dates.append(current_date)
            highs.append(high)
            winds.append(wind)

    print(highs)


fig = plt.figure(dpi=128,figsize=(10,6))
ax = fig.add_subplot(1,1,1)
plt.plot(dates,highs,c='orange')
# plt.plot(dates,winds,c='blue')

plt.title('Daily high PM2.5')
plt.xlabel('',fontsize=16)
ax.xaxis.set_major_formatter(mdate.DateFormatter('%Y %b %d %H '))
plt.xticks(rotation=30) # freq='MS',设置刻度格式为每月的开始（month start frequency）
plt.ylabel('PM2.5',fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)


plt.show()